package com.biz.controller.basic;

import com.biz.model.Hmodel.TSysUser;
import com.biz.model.Pmodel.User;
import com.biz.model.Pmodel.sys.Puser;
import com.biz.service.basic.BizpersonServiceI;
import com.framework.controller.BaseController;
import com.framework.model.MessageBox;
import com.framework.model.Paging;
import com.framework.utils.MD5;
import com.framework.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 志愿队管理
 * volunteer ：志愿者
 * @author lzq
 * community
 */
@Controller
@RequestMapping("/bizperson")
public class BizpersonController extends BaseController {
    @Autowired
    private BizpersonServiceI bizpersonService;

    /**
     * 跳转志愿者管理页面
     * @param mv
     * @return
     */
    @RequestMapping("toBizperson")
    public ModelAndView toBizperson(ModelAndView mv){
        mv.setViewName("basic/biz/bizperson");
        return mv;
    }

    @RequestMapping("showDatas")
    public void showDatas(HttpServletResponse response, HttpServletRequest request)throws Exception{
        //前端封装的查询参数
        Map map = getParameterByRequest(request);
        User user =  (User)getShiroAttribute("user");
        map.put("userId", user.getId());
        Paging<Puser> paging = bizpersonService.queryBizpersons(map);
        writeJson(paging, response);
    }

    @RequestMapping("doSave")
    public void doSave(HttpServletResponse response, HttpServletRequest request,Puser puser) {
        MessageBox mb = getBox();
        User user = (User)getShiroAttribute("user");
        puser.setMapId(user.getId());
        try {
            bizpersonService.addOrUpdateByPuser(puser);
            mb.setSuccess(true);
        } catch (Exception e) {
            mb.setSuccess(false);
            mb.setMsg(e.getMessage());
            e.printStackTrace();
        }


        writeJson(mb,response);
    }

    @RequestMapping("toEdit")
    public ModelAndView toEdit(String id) throws Exception {
        mv.clear();
        TSysUser tSysUser = bizpersonService.getTsysUserById(id);
        mv.addObject("tSysUser",tSysUser);
        mv.setViewName("basic/biz/biz_detail");
        return mv;
    }

    @RequestMapping("deByIds")
    public void delGridById(HttpServletResponse response, HttpServletRequest request,String ids) throws Exception {
        MessageBox mb = getBox();
        try{
            bizpersonService.executeHql(StringUtil.formateString("update TSysUser set isdel =1 where id in ({0})",StringUtil.formatSqlIn(ids)));
            mb.setSuccess(true);
        }catch (Exception e){
            mb.setSuccess(false);
        }
        writeJson(mb,response);
    }

    @RequestMapping("resetPwd")
    public void resetPwd(HttpServletResponse response,String ids) throws Exception {
        MessageBox mb = getBox();
        String pwd = MD5.md5("123456");
        try{
            bizpersonService.executeHql(StringUtil.formateString("update TSysUser set pwd ='{0}' where id in ({1})",pwd,StringUtil.formatSqlIn(ids)));
            mb.setSuccess(true);
        }catch (Exception e){
            mb.setSuccess(false);
        }
        writeJson(mb,response);
    }

    /**
     * 跳转管理者会员数量
     * @param mv
     * @return
     */
    @RequestMapping("showList")
    public ModelAndView showList(ModelAndView mv,String id){
        mv.addObject("id",id);
        mv.setViewName("basic/biz/bizMebList");
        return mv;
    }

    @RequestMapping("showBizMebList")
    public void showBizMebList(HttpServletResponse response, HttpServletRequest request)throws Exception{
        //前端封装的查询参数
        Map map = getParameterByRequest(request);
        Paging<Puser> paging = bizpersonService.showBizMebList(map);

        writeJson(paging, response);
    }

}
